home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xfig.idb / usr / freeware / src / xfig / xfig.3.1.4 / main.c.z / main.c
Encoding:
C/C++ Source or Header  |  1997-09-09  |  35.2 KB  |  1,064 lines

  1. /*
  2.  * FIG : Facility for Interactive Generation of figures
  3.  * Copyright (c) 1985 by Supoj Sutanthavibul
  4.  * Parts Copyright (c) 1991 by Paul King
  5.  * Parts Copyright (c) 1994 by Brian V. Smith
  6.  *
  7.  * The X Consortium, and any party obtaining a copy of these files from
  8.  * the X Consortium, directly or indirectly, is granted, free of charge, a
  9.  * full and unrestricted irrevocable, world-wide, paid up, royalty-free,
  10.  * nonexclusive right and license to deal in this software and
  11.  * documentation files (the "Software"), including without limitation the
  12.  * rights to use, copy, modify, merge, publish, distribute, sublicense,
  13.  * and/or sell copies of the Software, and to permit persons who receive
  14.  * copies from any such party to do so, with the only requirement being
  15.  * that this copyright notice remain intact.  This license includes without
  16.  * limitation a license to do the foregoing actions under any patents of
  17.  * the party supplying this software to the X Consortium.
  18.  */
  19.  
  20. #include "fig.h"
  21. #include "figx.h"
  22. #include "version.h"
  23. #include "patchlevel.h"
  24. #include "resources.h"
  25. #include "object.h"
  26. #include "mode.h"
  27. #include "u_fonts.h"
  28. #include "w_color.h"
  29. #include "w_drawprim.h"
  30. #include "w_mousefun.h"
  31. #include "w_setup.h"
  32. #include "w_util.h"
  33. #ifdef USE_XPM_ICON
  34. #include <xpm.h>
  35. #endif /* USE_XPM_ICON */
  36.  
  37. /* input extensions for an input tablet */
  38. #ifdef USE_TAB
  39. #include "X11/extensions/XInput.h"
  40. #endif
  41.  
  42. /************** EXTERNAL functions **************/
  43.  
  44. extern void    quit(), undo(), paste(), redisplay_canvas(), delete_all_cmd();
  45. extern void    popup_print_panel(), popup_file_panel(), popup_export_panel();
  46. extern void    do_load(), do_save(), popup_unit_panel();
  47. extern void    inc_zoom(), dec_zoom();
  48.  
  49. extern void    setup_cmd_panel();
  50. extern        X_error_handler();
  51. extern void    error_handler();
  52. extern void    my_quit();
  53. extern int    ignore_exp_cnt;
  54. extern int    psfontnum();
  55. extern int    latexfontnum();
  56.  
  57. #include "fig.icon.X"
  58. Pixmap        fig_icon;
  59.  
  60. static char    tool_name[100];
  61. static int    screen_res;
  62.  
  63. /************** FIG options ******************/
  64.  
  65. static char    *filename = NULL;
  66.  
  67. static Boolean    true = True;
  68. static Boolean    false = False;
  69. static int    Idefault = DEFAULT;
  70. static int    Izero = 0;
  71. static int    Ione = 1;
  72. static int    Itwo = 2;
  73. static float    Fzero = 0.0;
  74. static float    Fone = 1.0;
  75.  
  76. /* actions so that we may install accelerators at the top level */
  77. static XtActionsRec    main_actions[] =
  78. {
  79.     {"Quit", (XtActionProc) quit},
  80.     {"Delete_all", (XtActionProc) delete_all_cmd},
  81.     {"Undo", (XtActionProc) undo},
  82.     {"Redraw", (XtActionProc) redisplay_canvas},
  83.     {"Paste", (XtActionProc) paste},
  84.     {"File", (XtActionProc) popup_file_panel},
  85.       {"LoadFile", (XtActionProc) do_load},
  86.       {"SaveFile", (XtActionProc) do_save},
  87.     {"Export", (XtActionProc) popup_export_panel},
  88.     {"Print", (XtActionProc) popup_print_panel},
  89.     {"Units", (XtActionProc) popup_unit_panel},
  90. };
  91.  
  92. static XtResource application_resources[] = {
  93.     {"iconGeometry",  "IconGeometry",  XtRString,  sizeof(char *),
  94.     XtOffset(appresPtr,iconGeometry), XtRString, (caddr_t) NULL},
  95.     {"showallbuttons", "ShowAllButtons", XtRBoolean, sizeof(Boolean),
  96.     XtOffset(appresPtr, ShowAllButtons), XtRBoolean, (caddr_t) & false},
  97.     {XtNjustify, XtCJustify, XtRBoolean, sizeof(Boolean),
  98.     XtOffset(appresPtr, RHS_PANEL), XtRBoolean, (caddr_t) & false},
  99.     {"landscape", XtCOrientation, XtRBoolean, sizeof(Boolean),
  100.     XtOffset(appresPtr, landscape), XtRBoolean, (caddr_t) & true},
  101.     {"debug", "Debug", XtRBoolean, sizeof(Boolean),
  102.     XtOffset(appresPtr, DEBUG), XtRBoolean, (caddr_t) & false},
  103.     {"pwidth", XtCWidth, XtRFloat, sizeof(float),
  104.     XtOffset(appresPtr, tmp_width), XtRFloat, (caddr_t) & Fzero},
  105.     {"pheight", XtCHeight, XtRFloat, sizeof(float),
  106.     XtOffset(appresPtr, tmp_height), XtRFloat, (caddr_t) & Fzero},
  107.     {XtNreverseVideo, XtCReverseVideo, XtRBoolean, sizeof(Boolean),
  108.     XtOffset(appresPtr, INVERSE), XtRBoolean, (caddr_t) & false},
  109.     {"trackCursor", "Track", XtRBoolean, sizeof(Boolean),
  110.     XtOffset(appresPtr, TRACKING), XtRBoolean, (caddr_t) & true},
  111.     {"inches", "Inches", XtRBoolean, sizeof(Boolean),
  112.     XtOffset(appresPtr, INCHES), XtRBoolean, (caddr_t) & true},
  113.     {"boldFont", "Font", XtRString, sizeof(char *),
  114.     XtOffset(appresPtr, boldFont), XtRString, (caddr_t) NULL},
  115.     {"normalFont", "Font", XtRString, sizeof(char *),
  116.     XtOffset(appresPtr, normalFont), XtRString, (caddr_t) NULL},
  117.     {"buttonFont", "Font", XtRString, sizeof(char *),
  118.     XtOffset(appresPtr, buttonFont), XtRString, (caddr_t) NULL},
  119.     {"startlatexFont", "StartlatexFont", XtRString, sizeof(char *),
  120.     XtOffset(appresPtr, startlatexFont), XtRString, (caddr_t) NULL},
  121.     {"startpsFont", "StartpsFont", XtRString, sizeof(char *),
  122.     XtOffset(appresPtr, startpsFont), XtRString, (caddr_t) NULL},
  123.     {"startfontsize", "StartFontSize", XtRFloat, sizeof(float),
  124.     XtOffset(appresPtr, startfontsize), XtRFloat, (caddr_t) & Fzero},
  125.     {"internalborderwidth", "InternalBorderWidth", XtRInt, sizeof(int),
  126.     XtOffset(appresPtr, internalborderwidth), XtRInt, (caddr_t) & Izero},
  127.     {"starttextstep", "StartTextStep", XtRFloat, sizeof(float),
  128.     XtOffset(appresPtr, starttextstep), XtRFloat, (caddr_t) & Fzero},
  129.     {"startfillstyle", "StartFillStyle", XtRInt, sizeof(int),
  130.     XtOffset(appresPtr, startfillstyle), XtRInt, (caddr_t) & Idefault},
  131.     {"startlinewidth", "StartLineWidth", XtRInt, sizeof(int),
  132.     XtOffset(appresPtr, startlinewidth), XtRInt, (caddr_t) & Ione},
  133.     {"startgridmode", "StartGridMode", XtRInt, sizeof(int),
  134.     XtOffset(appresPtr, startgridmode), XtRInt, (caddr_t) & Izero},
  135.     {"latexfonts", "Latexfonts", XtRBoolean, sizeof(Boolean),
  136.     XtOffset(appresPtr, latexfonts), XtRBoolean, (caddr_t) & false},
  137.     {"specialtext", "SpecialText", XtRBoolean, sizeof(Boolean),
  138.     XtOffset(appresPtr, specialtext), XtRBoolean, (caddr_t) & false},
  139.     {"scalablefonts", "ScalableFonts", XtRBoolean, sizeof(Boolean),
  140.     XtOffset(appresPtr, SCALABLEFONTS), XtRBoolean, (caddr_t) & true},
  141.     {"monochrome", "Monochrome", XtRBoolean, sizeof(Boolean),
  142.     XtOffset(appresPtr, monochrome), XtRBoolean, (caddr_t) & false},
  143.     {"latexfonts", "Latexfonts", XtRBoolean, sizeof(Boolean),
  144.     XtOffset(appresPtr, latexfonts), XtRBoolean, (caddr_t) & false},
  145.     {"keyFile", "KeyFile", XtRString, sizeof(char *),
  146.     XtOffset(appresPtr, keyFile), XtRString, (caddr_t) "CompKeyDB"},
  147.     {"exportLanguage", "ExportLanguage", XtRString, sizeof(char *),
  148.     XtOffset(appresPtr, exportLanguage), XtRString, (caddr_t) "eps"},
  149.     {"flushleft", "FlushLeft", XtRBoolean, sizeof(Boolean),
  150.     XtOffset(appresPtr, flushleft), XtRBoolean, (caddr_t) & false},
  151.     {"userscale", "UserScale", XtRFloat, sizeof(float),
  152.     XtOffset(appresPtr, user_scale), XtRFloat, (caddr_t) & Fone},
  153.     {"userunit", "UserUnit", XtRString, sizeof(char *),
  154.     XtOffset(appresPtr, user_unit), XtRString, (caddr_t) ""},
  155.     {"but_per_row", "But_per_row", XtRInt, sizeof(int),
  156.     XtOffset(appresPtr, but_per_row), XtRInt, (caddr_t) & Itwo},
  157.     {"max_image_colors", "Max_image_colors", XtRInt, sizeof(int),
  158.     XtOffset(appresPtr, max_image_colors), XtRInt, (caddr_t) & Izero},
  159.     {"dont_switch_cmap", "Dont_switch_cmap", XtRBoolean, sizeof(Boolean),
  160.     XtOffset(appresPtr, dont_switch_cmap), XtRBoolean, (caddr_t) & false},
  161.     {"tablet", "Tablet", XtRBoolean, sizeof(Boolean),
  162.     XtOffset(appresPtr, tablet), XtRBoolean, (caddr_t) & false},
  163.     {"rulerthick", "RulerThick", XtRInt, sizeof(int),
  164.     XtOffset(appresPtr, rulerthick), XtRInt, (caddr_t) & Izero},
  165.     {"image_editor", "ImageEditor", XtRString, sizeof(char *),
  166.     XtOffset(appresPtr, image_editor), XtRString, (caddr_t) NULL},
  167. };
  168.  
  169. /* BE SURE TO UPDATE THE -help COMMAND OPTION LIST IF ANY CHANGES ARE MADE HERE */
  170.  
  171. static XrmOptionDescRec options[] =
  172. {
  173.     {"-iconGeometry", ".iconGeometry", XrmoptionSepArg, (caddr_t) NULL},
  174.     {"-showallbuttons", ".showallbuttons", XrmoptionNoArg, "True"},
  175.     {"-right", ".justify", XrmoptionNoArg, "True"},
  176.     {"-left", ".justify", XrmoptionNoArg, "False"},
  177.     {"-debug", ".debug", XrmoptionNoArg, "True"},
  178.     {"-landscape", ".landscape", XrmoptionNoArg, "True"},
  179.     {"-Landscape", ".landscape", XrmoptionNoArg, "True"},
  180.     {"-portrait", ".landscape", XrmoptionNoArg, "False"},
  181.     {"-Portrait", ".landscape", XrmoptionNoArg, "False"},
  182.     {"-pwidth", ".pwidth", XrmoptionSepArg, 0},
  183.     {"-pheight", ".pheight", XrmoptionSepArg, 0},
  184.     {"-inverse", ".reverseVideo", XrmoptionNoArg, "True"},
  185.     {"-notrack", ".trackCursor", XrmoptionNoArg, "False"},
  186.     {"-track", ".trackCursor", XrmoptionNoArg, "True"},
  187.     {"-inches", ".inches", XrmoptionNoArg, "True"},
  188.     {"-imperial", ".inches", XrmoptionNoArg, "True"},
  189.     {"-centimeters", ".inches", XrmoptionNoArg, "False"},
  190.     {"-metric", ".inches", XrmoptionNoArg, "False"},
  191.     {"-boldFont", ".boldFont", XrmoptionSepArg, 0},
  192.     {"-normalFont", ".normalFont", XrmoptionSepArg, 0},
  193.     {"-buttonFont", ".buttonFont", XrmoptionSepArg, 0},
  194.     {"-startpsFont", ".startpsFont", XrmoptionSepArg, 0},
  195.     {"-startlatexFont", ".startlatexFont", XrmoptionSepArg, 0},
  196.     {"-startFontSize", ".startfontsize", XrmoptionSepArg, 0},
  197.     {"-startfontsize", ".startfontsize", XrmoptionSepArg, 0},
  198.     {"-latexfonts", ".latexfonts", XrmoptionNoArg, "True"},
  199.     {"-specialtext", ".specialtext", XrmoptionNoArg, "True"},
  200.     {"-scalablefonts", ".scalablefonts", XrmoptionNoArg, "True"},
  201.     {"-noscalablefonts", ".scalablefonts", XrmoptionNoArg, "False"},
  202.     {"-monochrome", ".monochrome", XrmoptionNoArg, "True"},
  203.     {"-internalBW", ".internalborderwidth", XrmoptionSepArg, 0},
  204.     {"-internalBorderWidth", ".internalborderwidth", XrmoptionSepArg, 0},
  205.     {"-keyFile", ".keyFile", XrmoptionSepArg, 0},
  206.     {"-exportLanguage", ".exportLanguage", XrmoptionSepArg, 0},
  207.     {"-flushleft", ".flushleft", XrmoptionNoArg, "True"},
  208.     {"-center", ".flushleft", XrmoptionNoArg, "False"},
  209.     {"-userscale", ".userscale", XrmoptionSepArg, 0},
  210.     {"-userunit", ".userunit", XrmoptionSepArg, 0},
  211.     {"-but_per_row", ".but_per_row", XrmoptionSepArg, 0},
  212.     {"-starttextstep", ".starttextstep",  XrmoptionSepArg, 0},
  213.     {"-startfillstyle", ".startfillstyle", XrmoptionSepArg, 0},
  214.     {"-startlinewidth", ".startlinewidth", XrmoptionSepArg, 0},
  215.     {"-startgridmode", ".startgridmode",  XrmoptionSepArg, 0},
  216.     {"-max_image_colors", ".max_image_colors", XrmoptionSepArg, 0},
  217.     {"-dontswitchcmap", ".dont_switch_cmap", XrmoptionNoArg, "True"},
  218.     {"-tablet", ".tablet", XrmoptionNoArg, "True"},
  219.     {"-rulerthick", ".rulerthick", XrmoptionSepArg, 0},
  220.     {"-image_editor", ".image_editor", XrmoptionSepArg, 0},
  221. };
  222.  
  223. char *help_list =
  224.     "Usage:\n\
  225. xfig [-help] \
  226. [-boldFont <font>] \
  227. [-but_per_row <number>] \
  228. [-buttonFont <font>] \
  229. \n     \
  230. [-center] \
  231. [-centimeters] \
  232. [-flushleft] \
  233. [-debug] \
  234. [-dontswitchcmap] \
  235. \n     \
  236. [-exportLanguage <language>] \
  237. [-iconGeometry <geom>] \
  238. [-image_editor <editor>] \
  239. \n     \
  240. [-imperial] \
  241. [-inches] \
  242. [-internalBW <width>] \
  243. [-internalBorderWidth <width>] \
  244. \n     \
  245. [-inverse] \
  246. [-keyFile <file>] \
  247. [-landscape] \
  248. [-latexfonts] \
  249. [-left] \
  250. \n     \
  251. [-max_image_colors <number>] \
  252. [-metric] \
  253. [-monochrome] \
  254. [-normalFont <font>] \
  255. \n     \
  256. [-noscalablefonts] \
  257. [-notrack] \
  258. [-pheight <height>] \
  259. [-portrait] \
  260. \n     \
  261. [-pwidth <width>] \
  262. [-right] \
  263. [-rulerwidth <width>] \
  264. [-scalablefonts] \
  265. \n     \
  266. [-showallbuttons] \
  267. [-specialtext]\
  268. [-startfillstyle <style>] \
  269. \n     \
  270. [-startfontsize <size>] \
  271. [-startgridmode <number>] \
  272. [-startlatexFont <font>] \
  273. \n     \
  274. [-startlinewidth <width>] \
  275. [-startpsFont <font>] \
  276. [-starttextstep <number>] \
  277. \n     \
  278. [-tablet (if installed)] \
  279. [-track] \
  280. [-userscale <scale>] \
  281. \n     \
  282. [-userunit <units>] \
  283. [file]\n";
  284.  
  285. Atom wm_protocols[2];
  286.  
  287. static void    check_for_resize();
  288. static void    check_colors();
  289. XtActionsRec    form_actions[] =
  290. {
  291.     {"ResizeForm", (XtActionProc) check_for_resize},
  292.     {"Quit", (XtActionProc) my_quit},
  293. };
  294.  
  295. extern void clear_text_key();
  296. extern void paste_panel_key();
  297. static XtActionsRec text_panel_actions[] =
  298. {
  299.     {"PastePanelKey", (XtActionProc) paste_panel_key} ,
  300.     {"EmptyTextKey", (XtActionProc) clear_text_key} ,
  301. };
  302.  
  303. static String    form_translations =
  304.             "<ConfigureNotify>:ResizeForm()\n";
  305. static String    tool_translations =
  306.             "<Message>WM_PROTOCOLS:Quit()\n";
  307.  
  308. #define NCHILDREN    9
  309. static Widget    form;
  310.  
  311. main(argc, argv)
  312.     int            argc;
  313.     char       *argv[];
  314.  
  315. {
  316.     Widget        children[NCHILDREN];
  317.     int            ichild;
  318.     int            init_canv_wd, init_canv_ht;
  319.     XWMHints       *wmhints;
  320.     int            i;
  321.     char       *userhome;
  322.     Dimension        w, h;
  323.     XGCValues        gcv;
  324.  
  325.     DeclareArgs(5);
  326.  
  327.     if (argc > 1 && strcmp(argv[1],"-help")==0) {
  328.     fprintf(stderr,"%s",help_list);
  329.     exit(0);
  330.     }
  331.  
  332.     /* we are not writing the figure to the bitmap */
  333.     writing_bitmap = False;
  334.  
  335.     /* get the current directory so we can go back here on abort */
  336.     get_directory(orig_dir);
  337.  
  338.     /* get the TMPDIR environment variable for temporary files */
  339.     if ((TMPDIR = getenv("XFIGTMPDIR"))==NULL)
  340.     TMPDIR = "/tmp";
  341.  
  342.     (void) sprintf(tool_name, " XFIG %s patchlevel %s (Protocol %s)",
  343.            FIG_VERSION, PATCHLEVEL, PROTOCOL_VERSION);
  344.     (void) strcat(file_header, PROTOCOL_VERSION);
  345.     tool = XtAppInitialize(&tool_app, (String) "Fig", (XrmOptionDescList) options,
  346.                (Cardinal) XtNumber(options),
  347. #if XtSpecificationRelease < 5
  348.                (Cardinal *) & argc,
  349.                (String *) argv,
  350. #else
  351.                &argc,
  352.                argv,
  353. #endif
  354.                (String *) NULL,
  355. #if XtSpecificationRelease < 5
  356.                (String *) NULL, 
  357. #else
  358.                (ArgList) NULL,
  359. #endif
  360.                (Cardinal) 0);
  361.  
  362.  
  363.     /* install actions to get to the functions with accelerators */
  364.     XtAppAddActions(tool_app, main_actions, XtNumber(main_actions));
  365.  
  366.     fix_converters();
  367.     XtGetApplicationResources(tool, &appres, application_resources,
  368.                   XtNumber(application_resources), NULL, 0);
  369.  
  370.     /* All option args have now been deleted, leaving other args. (from Gordon Ross) */
  371.     if (argc > 1) {
  372.         filename = argv[1];
  373.     }
  374.  
  375.     tool_d = XtDisplay(tool);
  376.     tool_s = XtScreen(tool);
  377.     tool_cm = DefaultColormapOfScreen(tool_s);
  378.     tool_sn = DefaultScreen(tool_d);
  379.     tool_cells = CellsOfScreen(tool_s);
  380.     screen_res = (int) ((float) WidthOfScreen(tool_s) /
  381.             ((appres.INCHES) ?
  382.                 ((float) WidthMMOfScreen(tool_s)/2.54) :
  383.                      WidthMMOfScreen(tool_s) ));
  384.  
  385.     /* filled in later */
  386.     tool_w = (Window) NULL;
  387.  
  388.     if (appres.iconGeometry != (char *) 0) {
  389.         int scr, x, y, junk;
  390.  
  391.         for(scr = 0;
  392.             tool_s != ScreenOfDisplay(tool_d, scr);
  393.             scr++);
  394.  
  395.         XGeometry(tool_d, scr, appres.iconGeometry,
  396.                   "", 0, 0, 0, 0, 0, &x, &y, &junk, &junk);
  397.         FirstArg(XtNiconX, x);
  398.         NextArg(XtNiconY, y);
  399.         SetValues(tool);
  400.     }
  401.  
  402.     /* setup the defaults or the user preferences */
  403.  
  404.     if (appres.max_image_colors == 0)
  405.     appres.max_image_colors = DEF_MAX_IMAGE_COLS;
  406.  
  407.     if (appres.startfontsize >= 1.0)
  408.     cur_fontsize = round(appres.startfontsize);
  409.  
  410.     /* allow "Modern" for "Sans Serif" and allow "SansSerif" (no space) */
  411.     if (appres.startlatexFont) {
  412.         if (strcmp(appres.startlatexFont,"Modern")==0 ||
  413.         strcmp(appres.startlatexFont,"SansSerif")==0)
  414.           cur_latex_font = latexfontnum ("Sans Serif");
  415.     } else {
  416.         cur_latex_font = latexfontnum (appres.startlatexFont);
  417.     }
  418.  
  419.     cur_ps_font = psfontnum (appres.startpsFont);
  420.  
  421.     if (appres.starttextstep > 0.0)
  422.     cur_textstep = appres.starttextstep;
  423.  
  424.     if (appres.startfillstyle >= 0)
  425.     cur_fillstyle = min2(appres.startfillstyle,NUMFILLPATS-1);
  426.  
  427.     if (appres.startlinewidth >= 0)
  428.     cur_linewidth = min2(appres.startlinewidth,MAXLINEWIDTH);
  429.  
  430.     if (appres.startgridmode >= 0)
  431.     cur_gridmode = min2(appres.startgridmode,GRID_3);
  432.  
  433.     /* turn off PSFONT_TEXT flag if user specified -latexfonts */
  434.     if (appres.latexfonts)
  435.     cur_textflags = cur_textflags & (~PSFONT_TEXT);
  436.     if (appres.specialtext)
  437.     cur_textflags = cur_textflags | SPECIAL_TEXT;
  438.  
  439.     /* turn off PSFONT_TEXT flag if user specified -latexfonts */
  440.     if (appres.latexfonts)
  441.     cur_textflags = cur_textflags & (~PSFONT_TEXT);
  442.  
  443.     if (appres.user_unit)
  444.     strncpy(cur_fig_units, appres.user_unit, sizeof(cur_fig_units));
  445.     else
  446.     cur_fig_units[0] = '\0';
  447.  
  448.     /* assume color to start */
  449.     all_colors_available = True;
  450.  
  451.     /* check if monochrome screen */
  452.     if (tool_cells == 2 || appres.monochrome) {
  453.     all_colors_available = False;
  454.     if (appres.INVERSE) {
  455.         XrmValue    value;
  456.         XrmDatabase    newdb = (XrmDatabase) 0, old;
  457.  
  458.         value.size = sizeof("White");
  459.         value.addr = "White";
  460.         XrmPutResource(&newdb, "xfig*borderColor", "String", &value);
  461.         value.size = sizeof("White");
  462.         value.addr = "White";
  463.         XrmPutResource(&newdb, "xfig*foreground", "String", &value);
  464.         value.size = sizeof("Black");
  465.         value.addr = "Black";
  466.         XrmPutResource(&newdb, "xfig*background", "String", &value);
  467.         old = XtDatabase(tool_d);
  468.         XrmMergeDatabases(newdb, &old);
  469.  
  470.         /* now set the tool part, since its already created */
  471.         FirstArg(XtNborderColor, WhitePixelOfScreen(tool_s));
  472.         NextArg(XtNforeground, WhitePixelOfScreen(tool_s));
  473.         NextArg(XtNbackground, BlackPixelOfScreen(tool_s));
  474.         SetValues(tool);
  475.     }
  476.     }
  477.  
  478.     init_font();
  479.  
  480.     gc = DefaultGC(tool_d, tool_sn);
  481.     /* set the roman font for the message window */
  482.     XSetFont(tool_d, gc, roman_font->fid);
  483.  
  484.     /* make a gc for the command buttons */
  485.     gcv.font = button_font->fid;
  486.     button_gc = XCreateGC(tool_d, DefaultRootWindow(tool_d), GCFont, &gcv);
  487.     /* copy the other components from the default gc to the button_gc */
  488.     XCopyGC(tool_d, gc, ~GCFont, button_gc);
  489.  
  490.     /*
  491.      * check if the NUM_STD_COLS drawing colors could be allocated and have
  492.      * different palette entries
  493.      */
  494.     check_colors();
  495.  
  496.     init_cursor();
  497.     FirstArg(XtNinput, (XtArgVal) True);
  498.     NextArg(XtNdefaultDistance, (XtArgVal) 0);
  499.     NextArg(XtNresizable, (XtArgVal) False);
  500.     /* if we have already switched colormaps in check_colors() */
  501.     if (tool_cm)
  502.     NextArg(XtNcolormap, tool_cm);
  503.     form = XtCreateManagedWidget("form", formWidgetClass, tool,
  504.                  Args, ArgCount);
  505.  
  506.     if (INTERNAL_BW == 0)
  507.     INTERNAL_BW = appres.internalborderwidth;
  508.     if (INTERNAL_BW <= 0)
  509.     INTERNAL_BW = DEF_INTERNAL_BW;
  510.  
  511.     /* get the desired number of buttons per row for the mode panel */
  512.     SW_PER_ROW = appres.but_per_row;
  513.     if (SW_PER_ROW <= 0)
  514.     SW_PER_ROW = DEF_SW_PER_ROW;
  515.     else if (SW_PER_ROW > 6)
  516.     SW_PER_ROW = 6;
  517.  
  518.     init_canv_wd = appres.tmp_width *
  519.     (appres.INCHES ? PIX_PER_INCH : PIX_PER_CM)/ZOOM_FACTOR;
  520.     init_canv_ht = appres.tmp_height *
  521.     (appres.INCHES ? PIX_PER_INCH : PIX_PER_CM)/ZOOM_FACTOR;
  522.  
  523.     RULER_WD = appres.rulerthick;
  524.     if (RULER_WD < DEF_RULER_WD)
  525.     RULER_WD = DEF_RULER_WD;
  526.  
  527.     if (init_canv_wd == 0)
  528.     init_canv_wd = appres.landscape ? DEF_CANVAS_WD_LAND :
  529.         DEF_CANVAS_WD_PORT;
  530.  
  531.     if (init_canv_ht == 0)
  532.     init_canv_ht = appres.landscape ? DEF_CANVAS_HT_LAND :
  533.         DEF_CANVAS_HT_PORT;;
  534.  
  535.     setup_sizes(init_canv_wd, init_canv_ht);
  536.     (void) init_cmd_panel(form);
  537.     (void) init_msg(form,filename);
  538.     (void) init_mousefun(form);
  539.     (void) init_mode_panel(form);
  540.     (void) init_topruler(form);
  541.     (void) init_canvas(form);
  542.     (void) init_fontmenu(form); /* printer font menu */
  543.     (void) init_unitbox(form);
  544.     (void) init_sideruler(form);
  545.     (void) init_ind_panel(form);
  546.  
  547.     ichild = 0;
  548.     children[ichild++] = cmd_panel;    /* command buttons */
  549.     children[ichild++] = mousefun;    /* labels for mouse fns */
  550.     children[ichild++] = msg_form;    /* message window form */
  551.     children[ichild++] = mode_panel;    /* current mode */
  552.     children[ichild++] = topruler_sw;    /* top ruler */
  553.     children[ichild++] = unitbox_sw;    /* box containing units */
  554.     children[ichild++] = sideruler_sw;    /* side ruler */
  555.     children[ichild++] = canvas_sw;    /* main drawing canvas */
  556.     children[ichild++] = ind_panel;    /* current settings indicators */
  557.  
  558.     /*
  559.      * until the following XtRealizeWidget() is called, there are NO windows
  560.      * in existence
  561.      */
  562.  
  563.     XtManageChildren(children, NCHILDREN);
  564.     XtRealizeWidget(tool);
  565.     tool_w = XtWindow(tool);
  566.  
  567.     /* make sure we have the most current colormap */
  568.     set_cmap(tool_w);
  569.  
  570.     /* get this one for other sub windows too */
  571.     wm_delete_window = XInternAtom(XtDisplay(tool), "WM_DELETE_WINDOW", False);
  572.  
  573.     /* for the main window trap delete window and save_yourself (my_quit) is called */
  574.     wm_protocols[0] = wm_delete_window;
  575.     wm_protocols[1] = XInternAtom(XtDisplay(tool), "WM_SAVE_YOURSELF", False);
  576.     (void) XSetWMProtocols(XtDisplay(tool), tool_w, wm_protocols, 2);
  577.  
  578.     /* use the XPM color icon for color display */
  579. #ifdef USE_XPM_ICON
  580.     if (all_colors_available) {
  581.     Pixmap        dum;
  582.     Window        iconWindow;
  583.     int        status;
  584.  
  585.     /*  make a window for the icon */
  586.     iconWindow = XCreateSimpleWindow(tool_d, DefaultRootWindow(tool_d),
  587.                      0, 0, 1, 1, 0,
  588.                      BlackPixelOfScreen(tool_s),
  589.                      BlackPixelOfScreen(tool_s));
  590.     xfig_icon_attr.valuemask = XpmReturnPixels | XpmCloseness;
  591.     xfig_icon_attr.colormap = tool_cm;
  592.     /* flag whether or not to free colors when quitting xfig */
  593.     xfig_icon_attr.npixels = 0;
  594.     status = XpmCreatePixmapFromData(tool_d, iconWindow,
  595.                      fig_c_icon_X, &fig_icon, &dum, &xfig_icon_attr);
  596.     /* if all else fails, use standard monochrome bitmap for icon */
  597.     if (status == XpmSuccess) {
  598.         XResizeWindow(tool_d, iconWindow,
  599.               xfig_icon_attr.width,
  600.               xfig_icon_attr.height);
  601.         XSetWindowBackgroundPixmap(tool_d, iconWindow, fig_icon);
  602.         XtVaSetValues(tool, XtNiconWindow, iconWindow, NULL);
  603.     } else {
  604.         fig_icon = XCreateBitmapFromData(tool_d, tool_w,
  605.                      (char *) fig_bits, fig_width, fig_height);
  606.     }
  607.     } else {
  608. #endif /* USE_XPM_ICON */
  609.     fig_icon = XCreateBitmapFromData(tool_d, tool_w,
  610.                      (char *) fig_bits, fig_width, fig_height);
  611. #ifdef USE_XPM_ICON
  612.     }
  613. #endif /* USE_XPM_ICON */
  614.  
  615.     FirstArg(XtNtitle, tool_name);
  616.     NextArg(XtNiconPixmap, fig_icon);
  617.     SetValues(tool);
  618.     /* Set the input field to true to allow keyboard input */
  619.     wmhints = XGetWMHints(tool_d, tool_w);
  620.     wmhints->flags |= InputHint;/* add in input hint */
  621.     wmhints->input = True;
  622.     XSetWMHints(tool_d, tool_w, wmhints);
  623.     XFree((char *) wmhints);
  624.  
  625.     if (appres.RHS_PANEL) {    /* side button panel is on right size */
  626.     FirstArg(XtNfromHoriz, 0);
  627.     NextArg(XtNhorizDistance, SIDERULER_WD + INTERNAL_BW);
  628.     SetValues(topruler_sw);
  629.  
  630.     FirstArg(XtNfromHoriz, 0);
  631.     NextArg(XtNhorizDistance, 0);
  632.     NextArg(XtNfromVert, topruler_sw);
  633.     NextArg(XtNleft, XtChainLeft);    /* chain to left of form */
  634.     NextArg(XtNright, XtChainLeft);
  635.     SetValues(sideruler_sw);
  636.  
  637.     FirstArg(XtNfromHoriz, 0);
  638.     NextArg(XtNhorizDistance, 0);
  639.     NextArg(XtNfromVert, msg_form);
  640.     NextArg(XtNleft, XtChainLeft);    /* chain to left of form */
  641.     NextArg(XtNright, XtChainLeft);
  642.     SetValues(unitbox_sw);
  643.  
  644.     /* relocate the side button panel */
  645.     XtUnmanageChild(mode_panel);
  646.     XtUnmanageChild(canvas_sw);
  647.     FirstArg(XtNfromHoriz, canvas_sw);    /* panel right of canvas */
  648.     NextArg(XtNhorizDistance, -INTERNAL_BW);
  649.     NextArg(XtNfromVert, mousefun);
  650.     NextArg(XtNleft, XtChainRight);
  651.     NextArg(XtNright, XtChainRight);
  652.     SetValues(mode_panel);
  653.     FirstArg(XtNfromHoriz, sideruler_sw);    /* panel right of canvas */
  654.     SetValues(canvas_sw);
  655.     XtManageChild(canvas_sw);
  656.     XtManageChild(mode_panel);
  657.     }
  658.  
  659.     init_gc();
  660.  
  661.     /* get the size of the whole shebang */
  662.     FirstArg(XtNwidth, &w);
  663.     NextArg(XtNheight, &h);
  664.     GetValues(tool);
  665.     TOOL_WD = (int) w;
  666.     TOOL_HT = (int) h;
  667.  
  668.     setup_cmd_panel();
  669.     setup_msg();
  670.     setup_canvas();
  671.     setup_rulers();
  672.     setup_mode_panel();
  673.     setup_mousefun();
  674.     setup_fontmenu();        /* setup bitmaps in printer font menu */
  675.     setup_ind_panel();
  676.     get_directory(cur_dir);
  677.  
  678.     /* parse the export language resource */
  679.     for (i=0; i<NUM_EXP_LANG; i++)
  680.     if (strcmp(appres.exportLanguage, lang_items[i])==0)
  681.         break;
  682.     /* found it set the language number */
  683.     if (i < NUM_EXP_LANG)
  684.     cur_exp_lang = i;
  685.     else
  686.     file_msg("Unknown export language: %s, using default: %s",
  687.         appres.exportLanguage, lang_items[cur_exp_lang]);
  688.  
  689.     /* install the accelerators - cmd_panel, ind_panel and mode_panel
  690.     accelerators are installed in their respective setup_xxx procedures */
  691.     XtInstallAllAccelerators(canvas_sw, tool);
  692.     XtInstallAllAccelerators(mousefun, tool);
  693.     XtInstallAllAccelerators(msg_form, tool);
  694.     XtInstallAllAccelerators(topruler_sw, tool);
  695.     XtInstallAllAccelerators(sideruler_sw, tool);
  696.     XtInstallAllAccelerators(unitbox_sw, tool);
  697.     XtInstallAllAccelerators(ind_panel, tool);
  698.     XtInstallAllAccelerators(mode_panel, tool);
  699.  
  700.     XtAppAddActions(tool_app, form_actions, XtNumber(form_actions));
  701.     XtAppAddActions(tool_app, text_panel_actions, XtNumber(text_panel_actions));
  702.     XtOverrideTranslations(tool, XtParseTranslationTable(tool_translations));
  703.     XtOverrideTranslations(form, XtParseTranslationTable(form_translations));
  704.  
  705.     if (!appres.DEBUG) {
  706.     XSetErrorHandler(X_error_handler);
  707.     XSetIOErrorHandler((XIOErrorHandler) X_error_handler);
  708.     }
  709.  
  710.     (void) signal(SIGHUP, error_handler);
  711.     (void) signal(SIGFPE, error_handler);
  712. #ifdef SIGBUS
  713.     (void) signal(SIGBUS, error_handler);
  714. #endif
  715.     (void) signal(SIGSEGV, error_handler);
  716.     (void) signal(SIGINT, SIG_IGN);    /* in case user accidentally types ctrl-c */
  717.  
  718.     put_msg("READY, please select a mode or load a file");
  719.  
  720.     /*
  721.      * decide on filename for cut buffer: first try users HOME directory to
  722.      * allow cutting and pasting between sessions, if this fails create
  723.      * unique filename in /tmp dir
  724.      */
  725.  
  726.     userhome = getenv("HOME");
  727.     if (userhome != NULL && *strcpy(cut_buf_name, userhome) != '\0') {
  728.     strcat(cut_buf_name, "/.xfig");
  729.     } else {
  730.     sprintf(cut_buf_name, "%s%06d", "/tmp/xfig", getpid());
  731.     }
  732.  
  733.     if (filename == NULL)
  734.     strcpy(cur_filename, DEF_NAME);
  735.     else
  736.     load_file(filename,0,0);
  737.     update_cur_filename(cur_filename);
  738.  
  739.     app_flush();
  740.  
  741.     /* If the user requests a tablet then do the set up for it */
  742.     /*   and handle the tablet XInput extension events */
  743.     /*   in a custom XtAppMainLoop gjl */
  744.  
  745.     if (appres.tablet) {
  746.  
  747. #ifndef USE_TAB
  748.     file_msg("Input tablet not compiled in xfig - option ignored");
  749.     appres.tablet = False;
  750. #else
  751.  
  752. #define TABLETINCHES 11.7
  753. #define SETBUTEVT(d, e) ((d).serial = (e)->serial, \
  754.     (d).window     = (e)->window, (d).root = (e)->root, \
  755.     (d).subwindow     = (e)->subwindow, (d).time = (e)->time, \
  756.     (d).x         = (e)->axis_data[0] / max2(tablet_res, 0.1), \
  757.     (d).state     = (e)->state, \
  758.     (d).y         = ((int) ht - (e)->axis_data[1] / max2(tablet_res, 0.1)), \
  759.     (d).button     = (e)->button)
  760. /* Switch buttons because of the layout of the buttons on the mouse */
  761. #define SWITCHBUTTONS(d) ((d).button = ((d).button == Button2) ? Button1 : \
  762.         ((d).button == Button1) ? Button2 : \
  763.         ((d).button == Button4) ? Button3 : Button4)
  764.  
  765.     XEventClass eventList[3];
  766.     XEvent event;
  767.     XDeviceMotionEvent *devmotevt;
  768.     XDeviceButtonEvent *devbutevt;
  769.     XDevice        *tablet;
  770.     XDeviceState    *tabletState;
  771.     XValuatorState    *valState;
  772.     int i, numDevs, motiontype, butprstype, butreltype, dum;
  773.     long  minval, maxval;
  774.  
  775.     /* Get the device list */
  776.     XDeviceInfo *devPtr, *devInfo;
  777.     /* tablet_res is ratio between the tablet res and the screen res */
  778.     float tablet_res = 10.0;
  779.     /* ht is the height of the tablet at 100dpi */
  780.     Dimension ht, wd;
  781.  
  782.     XButtonEvent xprs, xrel;
  783.  
  784.     xprs.type = ButtonPress, xrel.type = ButtonRelease;
  785.     xprs.send_event = xprs.same_screen =
  786.       xrel.send_event = xrel.same_screen = True;
  787.     xprs.button = xrel.button = Button1;
  788.  
  789.     /* check if the XInputExtension exists */
  790.     if (!XQueryExtension(tool_d, INAME, &dum, &dum, &dum))
  791.         goto notablet;
  792.  
  793.     /* now search the device list for the tablet */
  794.         devInfo = XListInputDevices(tool_d, &numDevs);
  795.     if (numDevs == 0)
  796.         goto notablet;
  797.     
  798.     /* Open the tablet device and select the event types */
  799.     for (i = 0, devPtr = devInfo; i < numDevs; i++, devPtr++)
  800.       if (! strcmp(devPtr->name, XI_TABLET))
  801.         if ((tablet = XOpenDevice(tool_d, devPtr->id)) == NULL)
  802.           printf("Unable to open tablet\n");
  803.  
  804.         else {
  805.           DeviceMotionNotify(tablet,  motiontype, eventList[0]);
  806.           DeviceButtonPress(tablet,   butprstype, eventList[1]);
  807.           DeviceButtonRelease(tablet, butreltype, eventList[2]);
  808.  
  809.           if (XSelectExtensionEvent(tool_d,
  810.                  XtWindow(canvas_sw), eventList, 3))
  811.             printf("Bad status on XSelectExtensionEvent\n");
  812.         }
  813.  
  814.     XFreeDeviceList(devInfo);
  815.  
  816.     /* Get the valuator data which should give the resolution */
  817.     /*   of the tablet in absolute mode (the default / what we want) */
  818.     /*   Problem with sgi array index (possibly word size related ) */
  819.     tabletState = XQueryDeviceState(tool_d, tablet);
  820.     valState = (XValuatorState *) tabletState->data;
  821.     for (i = 0; i < tabletState->num_classes; i++)
  822.       if ((int) valState->class == ValuatorClass)
  823.       {
  824.         if (valState->num_valuators)
  825.         {
  826. #if sgi
  827.           minval = valState->valuators[4];
  828.           maxval = valState->valuators[5];
  829. #else
  830.           minval = valState->valuators[0];
  831.           maxval = valState->valuators[1];
  832. #endif
  833.           tablet_res = ((float) maxval / TABLETINCHES / screen_res);
  834.           if (tablet_res <= 0.0 || tablet_res > 100.0)
  835.             tablet_res = 12.0;
  836.  
  837.               if (appres.DEBUG)
  838.             printf("TABLET: Res: %f %d %d %d %d\n", tablet_res,
  839.               valState->valuators[8], valState->valuators[10],
  840.               minval, maxval);
  841.         }
  842.       }
  843.       else
  844.         valState = (XValuatorState *)
  845.           ((long) valState + (int) valState->length);
  846.  
  847.     XFreeDeviceState(tabletState);
  848.  
  849.     xprs.display = xrel.display = tool_d;
  850.         FirstArg(XtNheight, &ht);
  851.         NextArg(XtNwidth, &wd);
  852.         GetValues(canvas_sw);
  853.     
  854.     /* "XtAppMainLoop" customized for extension events */
  855.         /* For tablet puck motion events use the location */
  856.         /* info to warp the cursor to the corresponding screen */
  857.         /* position.  For puck button events switch the buttons */
  858.         /* to correspond to the mouse buttons and send a mouse */
  859.         /* button event to the server so the program will just */
  860.         /* think it is getting a mouse button event and act */
  861.         /* appropriately */
  862.     for (;;) {
  863.       XtAppNextEvent(tool_app, &event);
  864.       if (event.type == motiontype) {
  865.         devmotevt = (XDeviceMotionEvent *) &event;
  866.             devmotevt->axis_data[0] /= tablet_res;
  867.             devmotevt->axis_data[1] /= tablet_res;
  868.  
  869.             /* Keep the pointer within the canvas window */
  870.             FirstArg(XtNheight, &ht);
  871.             NextArg(XtNwidth, &wd);
  872.             GetValues(canvas_sw);
  873.         XWarpPointer(tool_d, None, XtWindow(canvas_sw), None, None, 0, 0,
  874.          min2(devmotevt->axis_data[0], (int) wd),
  875.          max2((int) ht - devmotevt->axis_data[1], 0));
  876.       }
  877.       else if (event.type == butprstype) {
  878.         devbutevt = (XDeviceButtonEvent *) &event;
  879.         SETBUTEVT(xprs, devbutevt);
  880.             SWITCHBUTTONS(xprs);
  881.         XSendEvent(tool_d, PointerWindow, True,
  882.            ButtonPressMask, (XEvent *) &xprs);
  883.       }
  884.       else if (event.type == butreltype) {
  885.         devbutevt = (XDeviceButtonEvent *) &event;
  886.         SETBUTEVT(xrel, devbutevt);
  887.             SWITCHBUTTONS(xrel);
  888.         XSendEvent(tool_d, PointerWindow, True, 
  889.            ButtonReleaseMask, (XEvent *) &xrel);
  890.       }
  891.       else
  892.         XtDispatchEvent(&event);
  893.     }
  894. notablet:
  895.     file_msg("No input tablet present");
  896.  
  897. #endif /* USE_TAB */
  898.  
  899.     XtAppMainLoop(tool_app);
  900.     }
  901.     else
  902.     XtAppMainLoop(tool_app);
  903.     return 0;
  904. }
  905.  
  906. static void
  907. check_for_resize(tool, event, params, nparams)
  908.     Widget        tool;
  909.     XButtonEvent   *event;
  910.     String       *params;
  911.     Cardinal       *nparams;
  912. {
  913.     int            dx, dy;
  914.     XConfigureEvent *xc = (XConfigureEvent *) event;
  915.  
  916.     if (xc->width == TOOL_WD && xc->height == TOOL_HT)
  917.     return;        /* no size change */
  918.     dx = xc->width - TOOL_WD;
  919.     dy = xc->height - TOOL_HT;
  920.     TOOL_WD = xc->width;
  921.     TOOL_HT = xc->height;
  922.     resize_all(CANVAS_WD + dx, CANVAS_HT + dy);
  923. }
  924.  
  925. /* resize whole shebang given new canvas size (width,height) */
  926.  
  927. resize_all(width, height)
  928.     int width, height;
  929. {
  930.     DeclareArgs(3);
  931.     Dimension        b;
  932.  
  933.     setup_sizes(width, height);
  934.  
  935.     XawFormDoLayout(form, False);
  936.     ignore_exp_cnt++;        /* canvas is resized twice - redraw only once */
  937.  
  938.     FirstArg(XtNborderWidth, &b);
  939.     /* first redo the top panels */
  940.     GetValues(cmd_panel);
  941.     XtResizeWidget(cmd_panel, CMDPANEL_WD, CMDPANEL_HT, b);
  942.     GetValues(mousefun);
  943.     XtResizeWidget(mousefun, MOUSEFUN_WD, MOUSEFUN_HT, b);
  944.     XtUnmanageChild(mousefun);
  945.     resize_mousefun();
  946.     XtManageChild(mousefun);    /* so that it shifts with msg_panel */
  947.     /* resize the message form by setting the current filename */
  948.     update_cur_filename(cur_filename);
  949.  
  950.     /* now redo the center area */
  951.     XtUnmanageChild(mode_panel);
  952.     FirstArg(XtNheight, (MODEPANEL_SPACE + 1) / 2);
  953.     SetValues(d_label);
  954.     FirstArg(XtNheight, (MODEPANEL_SPACE) / 2);
  955.     SetValues(e_label);
  956.     XtManageChild(mode_panel);    /* so that it adjusts properly */
  957.  
  958.     FirstArg(XtNborderWidth, &b);
  959.     GetValues(canvas_sw);
  960.     XtResizeWidget(canvas_sw, CANVAS_WD, CANVAS_HT, b);
  961.     GetValues(topruler_sw);
  962.     XtResizeWidget(topruler_sw, TOPRULER_WD, TOPRULER_HT, b);
  963.     resize_topruler();
  964.     GetValues(sideruler_sw);
  965.     XtResizeWidget(sideruler_sw, SIDERULER_WD, SIDERULER_HT, b);
  966.     resize_sideruler();
  967.     XtUnmanageChild(sideruler_sw);
  968.     XtManageChild(sideruler_sw);/* so that it shifts with canvas */
  969.     XtUnmanageChild(unitbox_sw);
  970.     XtManageChild(unitbox_sw);    /* so that it shifts with canvas */
  971.  
  972.     XawFormDoLayout(form, True);
  973. }
  974.  
  975. static void
  976. check_colors()
  977. {
  978.     int            i;
  979.     XColor        dum,color;
  980.  
  981.     /* no need to allocate black and white specially */
  982.     colors[BLACK] = BlackPixelOfScreen(tool_s);
  983.     colors[WHITE] = WhitePixelOfScreen(tool_s);
  984.     /* fill the colors array with black (except for white) */
  985.     for (i=0; i<NUM_STD_COLS; i++)
  986.     if (i != BLACK && i != WHITE)
  987.         colors[i] = colors[BLACK];
  988.  
  989.     /* initialize user color cells */
  990.     for (i=0; i<MAX_USR_COLS; i++) {
  991.         colorFree[i] = True;
  992.         n_colorFree[i] = True;
  993.         num_usr_cols = 0;
  994.     }
  995.  
  996.     /* if monochrome resource is set, do not even check for colors */
  997.     if (!all_colors_available || appres.monochrome) {
  998.     return;
  999.     }
  1000.  
  1001.     for (i=0; i<NUM_STD_COLS; i++) {
  1002.     /* try to allocate another named color */
  1003.     /* first try by #xxxxx form if exists, then by name from rgb.txt file */
  1004.     if (!xallncol(colorNames[i+1].rgb,&color,&dum)) {
  1005.          /* can't allocate it, switch colormaps try again */
  1006.          if (!switch_colormap() || 
  1007.             (!xallncol(colorNames[i+1].rgb,&color,&dum))) {
  1008.             fprintf(stderr, "Not enough colormap entries available for basic colors\n");
  1009.             fprintf(stderr, "using monochrome mode.\n");
  1010.             all_colors_available = False;
  1011.             return;
  1012.         }
  1013.     }
  1014.     /* put the colorcell number in the color array */
  1015.     colors[i] = color.pixel;
  1016.     }
  1017. }
  1018.  
  1019. /* useful when using ups */
  1020. XSyncOn()
  1021. {
  1022.     XSynchronize(tool_d, True);
  1023.     XFlush(tool_d);
  1024. }
  1025.  
  1026. XSyncOff()
  1027. {
  1028.     XSynchronize(tool_d, False);
  1029.     XFlush(tool_d);
  1030. }
  1031.  
  1032. /* 
  1033.  * This will parse the hexadecimal form of the named colors in the standard color
  1034.  * names.  Some servers can't parse the hex form for XAllocNamedColor()
  1035.  */
  1036.  
  1037. int
  1038. xallncol(name,color,exact)
  1039.     char    *name;
  1040.     XColor    *color,*exact;
  1041. {
  1042.     unsigned    short r,g,b;
  1043.     char    nam[30];
  1044.  
  1045.     if (*name != '#')
  1046.     return XAllocNamedColor(tool_d,tool_cm,name,color,exact);
  1047.  
  1048.     /* gcc doesn't allow writing on constant strings without the -fwritable_strings
  1049.        option, and apparently some versions of sscanf need to write a char back */
  1050.     strcpy(nam,name);
  1051.     if (sscanf(nam,"#%2hx%2hx%2hx",&r,&g,&b) != 3 || nam[7] != '\0') {
  1052.     fprintf(stderr,
  1053.       "Malformed color specification %s in resources.c must be 6 hex digits",nam);
  1054.     exit(1);
  1055.     }
  1056.  
  1057.     color->red   = r<<8;
  1058.     color->green = g<<8;
  1059.     color->blue  = b<<8;
  1060.     color->flags = DoRed|DoGreen|DoBlue;
  1061.     *exact = *color;
  1062.     return XAllocColor(tool_d,tool_cm,color);
  1063. }
  1064.